<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/17
 * Time: 14:42
 */

namespace app\index\controller;
use app\common\model\Adv;
use app\common\model\Shop as ShopModel;
use app\common\model\Goods as GoodsModel;
use app\common\model\ShopCategory;
use app\common\model\SpecGoodsPrice;

class Shop extends Base
{
    public function shop_list(){
        //$where = ['apply_status'=>1];
        $where = [['apply_status','=',1]];
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;

        if($shop_name = isset($params['search_key'])){
            $where[] = ['shop_name','like',"%{$params['search_key']}%"];
        }

        if($cat_id = isset($params['cat_id'])){
            $where[] = ['cat_id','like',"%{$params['cat_id']}%"];
        }

        $condition = [
            'with'  => ['getMember'],
            'where' => $where,
            //'order' => ['id' => 'desc'],
            'field' => 'shop_id,shop_name,member_id,shop_logo,province_id,city_id,shop_group_name',
            'paginate' => [
                'page' => $page,
                'query' => $params,
                //'list_rows'=>1,
            ],
        ];

        $shop_count = ShopModel::where($where)->count('shop_id');
        $shop = ShopModel::getPageAll($condition)->each(function($item){
            $goods = GoodsModel::getAll(['is_on_sale'=>1,'shop_id'=>$item['shop_id']],['goods_id'=>'desc'],'goods_id,goods_name,shop_price,original_img,goods_type',4)->each(function($item2){
                if($item2['goods_type'] !=0){
                    $min = SpecGoodsPrice::where(['goods_id'=>$item2['goods_id']])->min('price');
                    $max = SpecGoodsPrice::where(['goods_id'=>$item2['goods_id']])->max('price');
                    $item2['shop_price'] = $min.'-'.$max;
                }
            });
            $item['goods'] = $goods;
        });

        //获取推荐商品
        $recommendGoods = GoodsModel::getRecommendGoods();
        $this->assign(compact('shop_count','shop','recommendGoods'));
        return $this->fetch();
    }

    public function shop_cover(){
        $shop_id = $this->request->param('shop_id');
        $shop = $this->findModel($shop_id);
        ShopModel::where(['shop_id'=>$shop_id])->with(['getMember'])->find();
        //取出分类
        $shopCategory = ShopCategory::getAllCategory($shop_id);
        //店铺广告
        $shop_adv = Adv::getAll(['shop_id'=>$shop_id]);
        //推荐商品
        $recommendGoods = GoodsModel::getRecommendGoods(['shop_id'=>$shop_id,'is_on_sale'=>1,'is_recommend'=>1],12);
        //新品上架
        $newGoods = GoodsModel::getRecommendGoods(['shop_id'=>$shop_id,'is_on_sale'=>1,'is_new'=>1],12);

        $this->assign(compact('shop','shopCategory','shop_adv','shop_id','recommendGoods','newGoods'));
        return $this->fetch();
    }


    /*
     * 店内产品列表
     * */
    public function shop_goods_list(){

        $shop_id = $this->request->param('shop_id');
        $shop = $this->findModel($shop_id);

        $cat_id = $this->request->param('cat_id',0);
        $params = array_filter(request()->param());
        $page = ! empty($params['page']) && $params['page'] >= 1 ? $params['page'] : 1;

        $where = [['is_on_sale','=',1]];

        if(!empty($cat_id)){
            $category = ShopCategory::getTree($cat_id);
            $son = array_column($category,'id');
            if(empty($son)){
                $son[] = $cat_id;
            }
           $where[] = ['shop_cat_id','in',$son];
        }
        if(!empty($params['figure_nu'])){
            $where[] = ['figure_nu','like',"%{$params['figure_nu']}%"];
        }
        if(!empty($param['min_price'])){
            $where[] = ['shop_price','>=',$param['min_price']];
        }
        if(!empty($param['max_price'])){
            $where[] = ['shop_price','<=',$param['max_price']];
        }

        if(!empty($param['min_price']) && !empty($param['max_price'])){
            $where[] = ['shop_price','between',[$param['min_price'],$param['max_price']]];
        }

        $condition = [
            //'with'  => [],
            'where' => $where,
            //'order' => ['id' => 'desc'],
            'field' => 'goods_id,goods_name,shop_price,original_img,goods_type,sales_sum,comment_count',
            'paginate' => [
                'page' => $page,
                'query' => $params,
                'list_rows'=>20,
            ],
        ];

        $goods = goodsModel::getPageAll($condition);
        $shop = $this->findModel($shop_id);
        //取出分类
        $shopCategory = ShopCategory::getAllCategory($shop_id);
        //店铺广告
        $shop_adv = Adv::getAll(['shop_id'=>$shop_id]);
        //推荐商品
        $recommendGoods = GoodsModel::getRecommendGoods(['shop_id'=>$shop_id,'is_on_sale'=>1,'is_recommend'=>1],12);

        $this->assign(compact('shop','shopCategory','shop_adv','shop_id','recommendGoods','goods'));
        return $this->fetch();
    }

    /**
     * 得到模型
     *
     * @param int $id
     * @param array $where
     *
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function findModel($id,$where = [])
    {
        if (empty($id) && empty($where)) {
            throw new \think\exception\RouteNotFoundException();
        }
        return ShopModel::findModel($id, $where);
    }
}